Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(field): add preserveOriginalLabel prop to SearchSelect #8919

Merged
merged 1 commit into from
Dec 17, 2024

Conversation

DBvc
Copy link
Contributor

@DBvc DBvc commented Dec 11, 2024

  • Add preserveOriginalLabel prop to control whether to use the original label when option is selected
  • Fix the issue where highlighted search text appears in the input box when option is selected

BREAKING CHANGE: None

close #8918

出现问题的原因:

在多选且开启 labelInValue 的情况,onChange 的时候会使用 getMergeValue 来合并最终数据

const getMergeValue: SelectProps<any>['onChange'] = (value, option) => {

代码如下:

// 多选模式开启 labelInValue 的时候生成最终变化的值的函数
const getMergeValue: SelectProps<any>['onChange'] = (value, option) => {
    if (Array.isArray(value) && Array.isArray(option) && value.length > 0) {
      // 多选情况且用户有选择
      return value.map((item, index) => {
        const optionItem = (option as DefaultOptionType[])?.[
          index
        ] as DefaultOptionType;
        const dataItem = optionItem?.['data-item'] || {};
        return {
          ...dataItem,
          ...item,
        };
      });
    }
    return [];
  };
  
  
  // 生成实际渲染的 options 的代码
  const genOptions = (
    mapOptions: RequestOptionsType[],
  ): DefaultOptionType[] => {
    return mapOptions.map((item, index) => {
      const {
        className: itemClassName,
        optionType,
        ...resetItem
      } = item as RequestOptionsType;

      const label = item[labelPropsName];
      const value = item[valuePropsName];
      const itemOptions = item[optionsPropsName] ?? [];

      if (optionType === 'optGroup' || item.options) {
        return {
          label: label,
          ...resetItem,
          data_title: label,
          title: label,
          key: value ?? `${label?.toString()}-${index}-${nanoid()}`, // 防止因key相同导致虚拟滚动出问题
          children: genOptions(itemOptions),
        } as DefaultOptionType;
      }

      return {
        title: label,
        ...resetItem,
        data_title: label,
        value: value ?? index,
        key: value ?? `${label?.toString()}-${index}-${nanoid()}`,
        'data-item': item,
        className: `${prefixCls}-option ${itemClassName || ''}`.trim(),
        label: optionItemRender?.(item as any) || label,
      } as DefaultOptionType;
    });
  };

渲染的下拉列表里的 label 是经过optionItemRender 处理过的用关键词匹配加上高亮的 ReactNode (https://github.com/ant-design/pro-components/blob/21ce755745c1b5fa26415cd1dcc1a8ac8ed2c159/packages/field/src/components/Select/index.tsx#L531),
这里 item 里的 label 会把 dataItem 的 label 覆盖掉,就导致最终填充到输入框的内容也是带高亮样式的

这个 PR 的解决方法:
添加了一个属性 preserveOriginalLabel, 开启这个属性后,在开启 labelInValue 的时候,触发 onChange 的时候的 value 里就用原始的 options 里的 label,关闭后就用经过 optionItemRender 处理过的实际展示出来的 label

Copy link

github-actions bot commented Dec 11, 2024

😭 Deploy PR Preview failed.

- Add preserveOriginalLabel prop to control whether to use the original label when option is selected
- Fix the issue where highlighted search text appears in the input box when option is selected

BREAKING CHANGE: None
@DBvc DBvc force-pushed the fix-search-select branch from ef1971b to 468bf0c Compare December 11, 2024 08:14
@DBvc DBvc changed the title feat(field): add preserveOriginalLabel prop to SearchSelect fix(field): add preserveOriginalLabel prop to SearchSelect Dec 11, 2024
@chenshuai2144 chenshuai2144 merged commit feb8200 into ant-design:master Dec 17, 2024
8 of 10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants